package com.iotproject.eduservice.service.impl;

import com.iotproject.commonutils.R;
import com.iotproject.eduservice.entity.EduCourse;
import com.iotproject.eduservice.entity.EduTeacher;
import com.iotproject.eduservice.service.EduCourseService;
import com.iotproject.eduservice.service.EduTeacherService;
import com.iotproject.eduservice.service.IndexService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IndexServiceImpl implements IndexService {
    @Autowired
    EduTeacherService eduTeacherService;

    @Autowired
    EduCourseService eduCourseService;

    @Cacheable(value = "index", key = "'indexList'")
    @Override
    public R index() {
        //前4名讲师
        QueryWrapper<EduTeacher> eduTeacherQueryWrapper = new QueryWrapper<>();
        eduTeacherQueryWrapper.orderByDesc("id");
        eduTeacherQueryWrapper.last("limit 4");
        List<EduTeacher> teacherList = eduTeacherService.list(eduTeacherQueryWrapper);

        //前8门课程
        QueryWrapper<EduCourse> eduCourseQueryWrapper = new QueryWrapper<>();
        eduCourseQueryWrapper.orderByDesc("id");
        eduCourseQueryWrapper.last("limit 4");
        List<EduCourse> eduList = eduCourseService.list(eduCourseQueryWrapper);

        return R.ok().data("eduList", eduList).data("teacherList", teacherList);
    }
}
